from http.server import BaseHTTPRequestHandler
from api.parse import *
import datetime
class handler(BaseHTTPRequestHandler):
	def do_GET(self):
		self.send_response(415)
		self.send_header('Content-type', 'text/plain')
		self.end_headers()
		self.wfile.write('{"err": 1, "msg": "send POST request instead"}'.encode())
		return
	def do_POST(self):
		self.send_response(200)
		self.send_header('Content-type', 'text/plain')
		t = datetime.datetime.now() + datetime.timedelta(days=365)
		if type_post(self) == "json":
			dat = parse_post(self)
			for key in dat:
				self.send_header('Set-Cookie', f"{key}={dat[key]}; expires={t.strftime('%a, %d-%b-%Y %H:%M:%S GMT')}; path=/")
		self.end_headers()
		self.wfile.write('{"err": 0, "msg": "cookie set"}'.encode())
		return
